package Snake;

import java.util.LinkedList;
import java.util.List;

import javax.swing.SizeSequence;

import Gui.Board;


public class Snake implements Runnable{
	
	private static final int TIME = 100;


	private Game game;
	private Cell head;
	private Cell goal;
	private LinkedList<Cell> sizeSnake = new LinkedList<Cell>();
	private LinkedList<Jump> listOfPossibleJumps = new LinkedList<Jump>();

	private Board board;
	
	public Snake(Cell head, Game game, LinkedList<Cell> sizeSnake, LinkedList<Jump> listOfPossibleJumps){ //precisa ter um objetivo de destino
		this.game=game;
//		this.goal=goal;
		this.head=head;
		this.sizeSnake=sizeSnake;
		this.listOfPossibleJumps=listOfPossibleJumps;
	}



	@Override
	public void run() {
		// System.out.println(sizeSnake.size());
		// int i=0;
		synchronized (sizeSnake) {
			while (!isOver() && !Thread.currentThread().isInterrupted()) {
				try {
						Thread.sleep((long) (TIME));
						System.out.println( "Tamanho da cobrar " +sizeSnake.size());
						System.out.println("Sou a "
								+ Thread.currentThread().getName() + " dorme: "
								+ ((long) (TIME)) + "ms"
								+ " Verificar dados para a movimentar-me!...");
						game.movimento(this);


				} catch (Exception e) {
					System.out.println(" Catch - GameOver!");
				}
			}
			System.err.println("O jogo acabou ? "  +isOver());
			System.out.println("isOver ACABOU!!!");

		}

	}
	
	private boolean isOver() {
		if(sizeSnake.getFirst().isValidToMove())
			return false;
		return true;
	}

	public LinkedList<Cell> getSizeSnake() {
		return sizeSnake;
	}

	public boolean isSnake(Cell cell) {
		for (int i = 0; i < sizeSnake.size(); i++) {
			if (sizeSnake.get(i).getX()== cell.getX() &&  sizeSnake.get(i).getY()== cell.getY()) {
				return true;
			}
		}
		return false;
	}

	
	

	
	public LinkedList<Jump> getListOfPossibleJumps() {
		return listOfPossibleJumps;
	}



	@Override
	public String toString() {
		return "Snake [game=" + game + ", head=" + head + ", goal=" + goal
				+ ", sizeSnake=" + sizeSnake + ", board=" + board + "]";
	}
	
	
	
	

}
